package com.xiaoshuidi.cloud.module.rooms.repository;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.PageParam;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.mybatis.core.util.MyBatisUtils;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.estimate.vo.EstimateListResp;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.estimate.vo.RelatedHouseReq;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomHouseEstimateMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomHouseEstimate;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @Author Feng
 * @date 2023/5/29
 */
@Repository
public class RoomHouseEstimateRepository extends ServiceImpl<RoomHouseEstimateMapper, RoomHouseEstimate> {

    public Page<RoomHouseEstimate> page1(String search, Integer pageNo, Integer pageSize) {
        Page<RoomHouseEstimate> page = Page.of(pageNo, pageSize);
        LambdaQueryWrapper<RoomHouseEstimate> wrapper = Wrappers.lambdaQuery();
        wrapper.and(StrUtil.isNotEmpty(search), s -> s.like(RoomHouseEstimate::getArea, search)
                .or()
                .like(RoomHouseEstimate::getRoomHouseNo, search));
        return this.page(page, wrapper);
    }

    public List<RoomHouseEstimate> getListByRoomId(RelatedHouseReq req) {
        LambdaQueryWrapper<RoomHouseEstimate> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouseEstimate::getRoomId, req.getRoomId());
        return this.baseMapper.selectList(wrapper);
    }

    public RoomHouseEstimate getEstimateByRoomId(Long roomHouseId) {
        LambdaQueryWrapper<RoomHouseEstimate> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomHouseEstimate::getRoomId, roomHouseId);
        List<RoomHouseEstimate> list = this.baseMapper.selectList(wrapper);
        if (null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    public PageResult<EstimateListResp> page2(String search, Integer pageNo, Integer pageSize) {
         PageParam pageParam = new PageParam().setPageNo(pageNo).setPageSize(pageSize);
        IPage<EstimateListResp> page = MyBatisUtils.buildPage(pageParam);
        List<EstimateListResp> list = this.baseMapper.selectListByConditions(page,search);
        PageResult<EstimateListResp> pageResult = new PageResult(list, page.getTotal());
        return pageResult;
    }
}
